package com.tianji.learning.mq;

import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domin.po.InteractionReply;
import com.tianji.learning.domin.po.PointsRecord;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.service.IInteractionReplyService;
import com.tianji.learning.service.ILearningLessonService;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class LessonChangeListener {

    private final ILearningLessonService learningLessonService;
    private final IPointsRecordService pointsRecordService;
    private final IInteractionReplyService interactionReplyService;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "learning.lesson.pay.queue", durable = "ture"),
            exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE),
            key = MqConstants.Key.ORDER_PAY_KEY))
    public void listenLessonPay(OrderBasicDTO order) {
        if (order == null || order.getUserId() == null || CollectionUtils.isEmpty(order.getCourseIds())) {
            // 数据有误，无需处理
            log.error("接收到MQ消息有误，订单数据为空");
            return;
        }
        // 2.添加课程
        log.debug("监听到用户{}的订单{}，需要添加课程{}到课表中", order.getUserId(), order.getOrderId(), order.getCourseIds());
        learningLessonService.addUserLessons(order.getUserId(), order.getCourseIds());
    }

    //签到积分
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("sign.points.queue"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.SIGN_IN))
    public void signRecords(PointsRecord pointsRecord) {

        log.debug("签到记录{}", pointsRecord);

        pointsRecordService.addPointsRecords(pointsRecord.getUserId(), pointsRecord.getPoints(), PointsRecordType.SIGN);
    }

    //点赞记录
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "qa.liked.times.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.QA_LIKED_TIMES_KEY))
    public void likeRecord(List<LikedTimesDTO> dtos) {
        log.debug("监听到点赞消息：{}", dtos);

        for (LikedTimesDTO dto : dtos) {
            InteractionReply interactionReply = interactionReplyService.getById(dto.getBizId());
            if (interactionReply == null) {
                return;
            }
            interactionReply.setLikedTimes(dto.getLikedTimes());
            interactionReplyService.updateById(interactionReply);
        }

    }

    //回答积分
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "reply.points.queue"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY))
    public void ReplyRecord(int points) {
        Long userId = UserContext.getUser();
        pointsRecordService.addPointsRecords(userId, points, PointsRecordType.QA);
    }

    //学习积分
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "learning.points.queue"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.LEARN_SECTION))
    public void learning(int points) {
        Long userId = UserContext.getUser();
        pointsRecordService.addPointsRecords(userId, points, PointsRecordType.LEARNING);
    }
}
